package com.xxx.demo.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xxx.demo.entity.News;
import com.xxx.demo.entity.ShowEntity;
import com.xxx.demo.mapper.NewsMapper;
import com.xxx.demo.service.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
* @author 562702
* @description 针对表【news(新闻表)】的数据库操作Service实现
* @createDate 2023-03-20 14:24:02
*/
@Service
public class NewsServiceImpl extends ServiceImpl<NewsMapper, News>
    implements NewsService {


    @Autowired(required = false)
    private NewsMapper newsMapper;

    @Override
    public List<News> queryNews() {
        return newsMapper.selectNews();
    }

    @Override
    public int saveNews(News news) {
        if (ObjectUtil.isEmpty(news)){
            return 0;
        }
        int insert = newsMapper.insert(news);
        return insert;
    }

    @Override
    public int removeNewById(Long id) {
        return newsMapper.deleteById(id);
    }

    /**
     * 逻辑删除
     * @param id
     * @return
     */
    @Override
    public int deleteNews(Long id){
//        News news = this.queryNewsById(id);
        News news = newsMapper.selectOne(new QueryWrapper<News>().eq("delete_flag", 0).eq("id", id));
        news.setDeleteFlag(1);
        int i = newsMapper.updateById(news);
        return i;
    }

    /**
     * 修改新闻
     * @param news
     */
    @Override
    @Transactional
    public void updateNews(News news) {
        newsMapper.updateById(news);
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @Override
    public News queryNewsById(Long id) {
        return newsMapper.selectById(id);
    }

    /**
     * 分页查询新闻
     * @param pageNo
     * @param pageSize
     * @param news
     * @return
     */
    @Override
    public IPage<News> selectNewsPage(Integer pageNo,Integer pageSize,News news){
        Page<News> newsIPage = new Page<>(pageNo,pageSize);
        return newsMapper.selectNewsPage(newsIPage,news);
    }

    /**
     * 查询所有新闻
     * @return
     */
    @Override
    public List<News> selectAllNews(){
        return newsMapper.selectList(new QueryWrapper<News>().eq("delete_flag",0));
    }
}




